import request from '@utils/request';
import * as actionType from './actionType';
import { message } from 'antd'
import moment from 'moment';
import  { getAllWarningRequest} from '../../WaringSearch/store/action'
export const getAllCamera = () => {
    return (dispatch) => {
        request('POST', '/api/server/web/rest/getAllVidicon', {
            cmd:"getAllVidicon",
            type:"request",
            request: {}
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                dispatch(getcamera(res.message))
            }
            else {
                message.error('获取摄像机信息失败！' + res.exception)
            }
        }).catch(err => console.log(err))
    }
}
//获取地图全部摄像机信息
const getcamera = (payload) => ({
    type: actionType.GETALLCAMERAS,
    payload
})
//根据摄像机id获取告警信息列表
export const getVidiconWarning = (information) =>{
    return (dispatch) => {
        request('POST', '/api/server/web/rest/warningDataStatistics/getVidiconWarning', {
            cmd: "getVidiconWarning",
            type: "request",
            request: {
                vidiconId: information.id,
                pageIndex: information.pageIndex,
                pageSize:information.pageSize
            }
        }).then(obj => {
            let res = obj.response;
            let result = res.message;
            let pageTotal= res.pageTotal
            let data = modifyingtimestamps(result);
            if (res.res) {
                dispatch(Alarmdetails(data,pageTotal))
            }
            else {
                message.error('获取摄像机告警列表失败！' + res.exception)
            }
        }).catch(err => console.log(err))
    }
}

const Alarmdetails = (data,pageTotal)=>({
    type: actionType.ALARMDETAILS,
    payload :data,
    pageTotal: pageTotal
})


//查询告警信息
export const getWarningByASTTMD2 = (information) => {
    return (dispatch) => {

        request('POST', '/api/server/web/rest/warning/getWarningByASTTMD', {
            cmd: 'getWarningByASTTMD',
            type: 'request',
            request: {
                ...information
            }
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                let totalCount=res.totalCount
                let result = res.message;
                let data = modifyingtimestamps(result);
                message.success('查找成功', 1);
                dispatch((Searchlarm(data,totalCount)))
            } else {
                message.error('查找失败' + res.exception, 1);
            }
        }).catch(err => console.log(err));
    }
}

const Searchlarm =(data,totalCount)=>({
    type:actionType.SEARCHALARM,
    payload:data,
    pageTotal:totalCount
})


//获取24小时历史告警信息
export const Gethistoricalalarms =(id)=>{
    return (dispatch) => {
        request('POST', '/api/server/web/rest/warningDataStatistics/twentyFourHoursWarning', {
            cmd: "twentyFourHoursWarning",
            type: "request",
            request: {
                vidiconId: id
            }
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                dispatch(history(res.message))
            }
            else {
                message.error('获取24小时告警信息失败！' + res.exception)
            }
        }).catch(err => console.log(err))
    }
}
const history = (payload)=>({
    type: actionType.GETHISTORY,
    payload
})
//根据告警id获取告警详情
export const GetAlarmdetails = (id)=>{

    return (dispatch) => {
        request('POST', '/api/server/web/rest/warning/getWarningById', {
            type: "request",
            cmd: "getWarningById",
            request: {
                "id": id
            }
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                dispatch(getAlarmdetails(res.message))
            }
            else {
                message.error('获取告警详情失败！' + res.exception)
            }
        }).catch(err => console.log(err))
    }
}
const getAlarmdetails=(payload)=>({
    type:actionType.GETALARMDETAILS,
    payload
})

//获取巡线工
const getalluser = (payload) => ({
    type: actionType.GETPATROLWORKERS,
    payload
})

export const getAlluserRequest = () => {
    return (dispatch) => {
        request('POST', '/api/server/web/rest/user/getAllUser', {
            cmd: 'getAllUser',
            type: 'request',
            request: {
            }
        }).then(obj => {
            let res = obj.response;
            let patrollerdata = res.message.patroller
            if (res.res) {   
                dispatch(getalluser(patrollerdata))
            } else {
                message.error('获取全部用户信息信息失败' + res.exception, 1);
            }
        }).catch(err => console.log(err));
    }
}

//派警

export const Distribute = (information) =>{
    return (dispatch) => {
        request('POST', '/api/server/web/rest/warning/Distribute', {
                type : "request",
                cmd : "Distribute",
                request : {
                    key:information.key,
                    basicDataId:information.basicDataId,
                    isDistribute:information.isDistribute
                 }
        }).then(obj => {
            let res = obj.response;
            let page
            if(information.currentpage!=information.currentpage){
            page ={
                    pageIndex:0,
                    pageSize:10
                }
            } 
            if(information.currentpage==information.currentpage){
            page ={
                pageIndex:information.currentpage,
                pageSize:10
            }}
            if (res.res) {   
                if(information.isDistribute=='distribute'){
                    message.success('派警成功')
                   
                }
                if(information.isDistribute=='notDistribute'){
                    message.success('处理成功')
                    
                }
                let data ={
                    id:information.camerId,
                    pageIndex:information.currentpage,
                    pageSize:10
                }
                dispatch(getVidiconWarning(data))
                dispatch(GetAlarmdetails(information.key))
                dispatch(getAllWarningRequest(page))
                dispatch(getAllCamera())
                dispatch(Gethistoricalalarms(information.camerId))
            } else {
                message.error('派警失败' + res.exception, 1);
            }
        }).catch(err => console.log(err));
    }
}
//将时间戳改成正常时间
const modifyingtimestamps =(data)=>{
    let array =data

    for(var i=0;i<data.length;i++){
        array[i].distributeTime=moment(data[i].distributeTime).format('YYYY-MM-DD HH:mm')
        array[i].feedbackTime=moment(data[i].feedbackTime).format('YYYY-MM-DD HH:mm')
        array[i].warningTime=moment(data[i].warningTime).format('YYYY-MM-DD HH:mm')
    }
    return array
}
//获取一周告警
export const getWeekWarning = (params) =>{
    return (dispatch) => {
        request('POST', '/api/server/web/rest/warningDataStatistics/getVidiconWeekWarning', {
            cmd: "getVidiconWeekWarning",
            type: "request",
            request: {
                vidiconId:params,
            }
        }).then(obj => {
            let res = obj.response;
            
            if (res.res) {
                dispatch(OneWeekWarn(res.message))
            }
            else {
                message.error('获取摄像机告警列表失败！' + res.exception)
            }
        }).catch(err => console.log(err))
    }
}
const OneWeekWarn=(payload)=>({
    type:actionType.GETWEEKWARN,
    payload
})